﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RabbitMQ.Client;

namespace Kugar.Tool.RemoteEventBus.ServerTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var connFactory = new ConnectionFactory()
            {
                HostName = "203.195.191.64",
                Port = 5672,
                VirtualHost = "developtest",
                UserName = "ylcoretest",
                Password = "123456",
                AutomaticRecoveryEnabled = true,
                RequestedHeartbeat = 10,
                NetworkRecoveryInterval = new TimeSpan(0, 0, 10)
            };

            var conn = connFactory.CreateConnection();
            conn.AutoClose = false;

            var bus = new RemoteEventBus();

            bus.Initialize(conn, "RemoteEvent_ServerTest");
            
            bus.NodeOnline += Bus_NodeOnline;

            var trigger=bus.GetAsyncEventTrigger<Test1EventArgs>("Test1");

            trigger.ClientHandledEvent += Trigger_ClientHandledEvent;

            var trigger2 = bus.GetSyncEventTrigger<Test2RaiseEventArgs>("Test2");
            trigger2.ClientHandledEvent += Trigger2_ClientHandledEvent;
            
            //bus.InitCompleted();

            Console.ReadLine();

            Console.WriteLine("触发异步事件");
            trigger.Trigger(new Test1EventArgs()
            {
                TestID = "sdfsdf"
            });


            Console.ReadLine();

            Console.WriteLine("触发同步事件");
            trigger2.Trigger(new Test2RaiseEventArgs()
            {
                TestID = "sdfsfsdf",
                Test2ID = "9999"
            });

            Console.WriteLine("同步调用结束");



            //var s1=bus.TriggerSyncEvent("SyncEventTest_2", new JObject()
            //    {
            //        ["A"] = 1
            //    }).Result;

            //foreach (var o in s1)
            //{
            //    Console.WriteLine(o.ToString(Formatting.None));
            //}



            //for (int i = 0; i < 10000; i++)
            //{
            //    var s1 = bus.TriggerSyncEvent("SyncEventTest_2", new JObject()
            //    {
            //        ["A"] = i
            //    }).Result;

            //    foreach (var o in s1)
            //    {
            //        Console.WriteLine(o.ToString(Formatting.None));
            //    }
            //}



            Console.ReadLine();
        }

        private static void Trigger2_ClientHandledEvent(object sender, Triggers.ClientEventHandeEventArgs e)
        {
            Console.WriteLine($"SyncRely={JsonConvert.SerializeObject(e)}");
        }

        private static void Trigger_ClientHandledEvent(object sender, Triggers.ClientEventHandeEventArgs e)
        {
            Console.WriteLine($"Rely={JsonConvert.SerializeObject(e)}");
        }

        private static void Bus_NodeOnline(object sender, NodeOnlineEventArgs e)
        {
            Console.WriteLine("Node Online:" + e.ServiceID);
        }

        public class Test1EventArgs : EventArgs
        {
            public string TestID { set; get; }
        }

        public class Test2RaiseEventArgs : EventArgs
        {
            public string TestID { set; get; }

            public string Test2ID { set; get; }
        }
    }
}
